package com.such.kit.database.bean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.such.kit.database.dialect.TypeFactory;
import com.such.kit.database.support.DDLBean;
import com.such.kit.datacast.CastCommons;
import com.such.kit.string.StringCommons;
import com.such.kit.validate.ValidateCommons;

public class DDLIndex extends DDLBean<List<DatabaseIndex>> {

	private static final long serialVersionUID = 7514914262954217118L;

	private String schema;
	private String tableName;
	private String indexName;
	private boolean unique;
	private List<String> columnNames = new ArrayList<String>();

	// 索引信息扩展
	private String changedIndexName;

	public DDLIndex() {
		
	}

	public DDLIndex(List<DatabaseIndex> indexs) {
		converter(indexs);
	}

	public String getSchema() {
		return this.schema;
	}

	public void setSchema(String schema) {
		this.schema = schema;
	}

	public String getTableName() {
		return this.tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public String getIndexName() {
		return this.indexName;
	}

	public void setIndexName(String indexName) {
		this.indexName = indexName;
	}

	public boolean getUnique() {
		return this.unique;
	}

	public void setUnique(boolean unique) {
		this.unique = unique;
	}

	public List<String> getColumnNames() {
		return this.columnNames;
	}

	public void setColumnNames(List<String> columnNames) {
		this.columnNames = columnNames;
	}

	public void addColumnName(String columnName) {
		this.columnNames.add(columnName);
	}

	public String getChangedIndexName() {
		return this.changedIndexName;
	}

	public void setChangedIndexName(String changedIndexName) {
		this.changedIndexName = changedIndexName;
	}

	public void converter(List<DatabaseIndex> indexs) {
		if (ValidateCommons.isNotEmpty(indexs)) {
			boolean first = true;
			String checkKey = "";
			for (DatabaseIndex index : indexs) {
				String schema = index.getSchema();
				String tableName = index.getTableName();
				String indexName = index.getIndexName();
				String nonUnique = index.getNonUnique();
				String key = StringCommons.merger(schema, tableName, indexName);
				if (first) {
					first = false;
					checkKey = StringCommons.merger(schema, tableName, indexName);
					setSchema(schema);
					setTableName(tableName);
					setIndexName(indexName);
					if (!CastCommons.stringToBoolean(nonUnique)) {
						setUnique(true);
					}
				}
				// 校验列是否属于一个索引
				if (key.equals(checkKey)) {
					addColumnName(index.getColumnName());
				}
			}
		}
	}

	public Map<String, String> buildRendererParams(Map<String, String> resourceKeyMapping, Map<String, String> ddlTemplateMapping, Map<String, String> params, TypeFactory type) {
		String tableName = this.tableName;
		if (ValidateCommons.isNotEmpty(this.schema) && ValidateCommons.isNotEmpty(tableName)) {
			tableName = StringCommons.merger(this.schema, ".", tableName);
		}

		Map<String, String> indexParams = new HashMap<String, String>();
		if (ValidateCommons.isNotEmpty(params)) {
			indexParams.putAll(params);
		}
		putString("tableName", tableName, indexParams, resourceKeyMapping, ddlTemplateMapping);
		putString("indexName", this.indexName, indexParams, resourceKeyMapping, ddlTemplateMapping);
		putBoolean("unique", this.unique, indexParams, resourceKeyMapping, ddlTemplateMapping);
		putList("columnNames", this.columnNames, indexParams, resourceKeyMapping, ddlTemplateMapping);
		putString("changedIndexName", this.changedIndexName, indexParams, resourceKeyMapping, ddlTemplateMapping);
		return indexParams;
	}

	/**
	 * <p>方法名：grouping</p>
	 * <p>描述：根据 schema、tableName、indexName 进行分组</p>
	 * @param indexs DatabaseIndex
	 * @return 分组列表
	 */
	public static Map<String, List<DatabaseIndex>> grouping(List<DatabaseIndex> indexs) {
		Map<String, List<DatabaseIndex>> indexsMap = new HashMap<String, List<DatabaseIndex>>();
		if (ValidateCommons.isNotEmpty(indexs)) {
			for (DatabaseIndex index : indexs) {
				String schema = index.getSchema();
				String tableName = index.getTableName();
				String indexName = index.getIndexName();
				if (ValidateCommons.isNotEmpty(indexName)) {
					String key = StringCommons.merger(schema, tableName, indexName);
					List<DatabaseIndex> tempIndexs = indexsMap.get(key);
					if (ValidateCommons.isEmpty(tempIndexs)) {
						tempIndexs = new ArrayList<DatabaseIndex>();
					}
					tempIndexs.add(index);
					indexsMap.put(key, tempIndexs);
				}
			}
		}
		return indexsMap;
	}

}
